/*
 *      Created on: March 9, 2011
 *      Author: topcoder
 */
#include <LengthUnit.h>
#include <sstream>

const unsigned int INCH_TOBASE = 1;
const unsigned int FEET_TOBASE = INCH_TOBASE * 12;
const unsigned int YARD_TOBASE = FEET_TOBASE * 3;
const unsigned int MILE_TOBASE = YARD_TOBASE * 1760;

static LengthUnit LengthArray[] = {INCH,FEET,YARD,MILE};
const unsigned int numOfLengthUnit = sizeof(LengthArray)/sizeof(LengthUnit);

LengthUnit& LengthUnit::getMile()
{
    static LengthUnit mile(MILE_TOBASE, "MILE");    
    return mile;
}
LengthUnit& LengthUnit::getYard()
{
    static LengthUnit yard(YARD_TOBASE, "YARD");
    return yard;
}
LengthUnit& LengthUnit::getFeet()
{
    static LengthUnit feet(FEET_TOBASE, "FEET");
    return feet;
}
LengthUnit& LengthUnit::getInch()
{
    static LengthUnit inch(INCH_TOBASE, "INCH");
    return inch;
}


LengthUnit::LengthUnit(unsigned int ratio, string name):Unit(ratio, name)
{
}

string LengthUnit::print(unsigned long quantity, const LengthUnit& unit)
{
    int loop = numOfLengthUnit;
    ostringstream tempString;

    while(loop--)
    {
        if(LengthArray[loop] <= unit)
        {
            tempString << LengthArray[loop].outputFormat(quantity);
            quantity = LengthArray[loop].getRemainder(quantity);
        }
    }
    return tempString.str();    
}
